<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
//include("sqlconnect.php");
//include("sqlgenerator.php");

//Semaphore properties
$key = 123456;
$max = 1;
$permissions = 0666;
$autoRelease = 1;


function store_ticket($ticket_no, $ticket_rate, $coupon_value) {
    //Open a new or get an existing semaphore
    $semaphore = sem_get($key, $max, $permissions, $autoRelease);
    if(!$semaphore) {
        echo "Failed on sem_get().\n";
    }
    sem_acquire($semaphore);

    db_connect();

    $query = get_ticketinfo($ticket_rate);
    $result = db_query($query);
    //echo $query;
    $row=mysql_fetch_array($result);
    try {
        $l0 = (float)$row['l0'];
        $l1 = (float)$row['l1'];
        $l2 = (float)$row['l2'];
    } catch (Exception $e) {
        $l0 = 2.9;
        $l1 = 0.6;
        $l2 = 0.5;
    }

    $query = get_level_info();
    $result = db_query($query);
    $i=0;
    while($row=mysql_fetch_array($result)) {
        $count[$i]=$row['count'];
        $i++;
    }

    $query=get_last_ticket();
    $result = db_query($query);
    $row = mysql_fetch_array($result);
    try {
        $level = (int)$row['level'];
        $parent_ticket_no = (int)$row['parent_ticket_no'];
    } catch (Exception $e) {
        $level = 0;
        $parent_ticket_no = '';
    }

    $query=get_num_childs($parent_ticket_no);
    $result = db_query($query);
    $num_childs = mysql_num_rows($result);

    $query=get_all_tickets_level($level);
    $result = db_query($query);
    $num_tickets_curr_level = mysql_num_rows($result);

    // Can be inserted in this level
    if($num_tickets_curr_level<$total[$level]) {
        // Can be inserted under the same parent
        if ($num_childs<$count[$level]) {
            // Nothing needs to be done here, it will be taken care
            // at the end of this function
        } else { // Needs to be inserted under the next parent
            $query=get_all_tickets_level($level-1);
            $result = db_query($query);

            $i=0;
            while($row=mysql_fetch_array($result)) {
                if($row['ticket_no']==$parent_ticket_no) {
                    break;
                }
                $i++;
            }

            // Can be inserted in the next parent in the same level
            //if($i<$count[$level-1] && ($row = mysql_fetch_array($result))) {
            if($row = mysql_fetch_array($result)) {
                $parent_ticket_no = $row['ticket_no'];
            } else { // Should be inserted in the next level
                $query = get_first_ticket_level($level);
                $result = db_query($query);
                $row = mysql_fetch_array($result);
                $parent_ticket_no = $row['ticket_no'];
                $level = $level + 1;
            }
        }
    } else { // Needs to be inserted at the next level
        $query = get_first_ticket_level($level);
        $result = db_query($query);
        $row = mysql_fetch_array($result);
        $parent_ticket_no = $row['ticket_no'];
        $level = $level + 1;
    }

    $fields[0]=$ticket_no;
    $fields[1]=$coupon_value;
    $fields[2]=0;
    $fields[3]=$parent_ticket_no;
    $fields[4]=$level;

    $query=insert_ticket($fields);
    $result = db_query($query);

    // Find the first four parents to add money
    $i = 0;
    do {
        $parent_ticket[$i] = $parent_ticket_no;
        
        $query = get_ticket($parent_ticket_no);
        $result = db_query($query);
        $row = mysql_fetch_array($result);
        if ($row) {
            $parent_ticket_no = $row['parent_ticket_no'];
        } else {
            break;
        }
        $parent_ticket[++$i] = $parent_ticket_no;
    } while ($parent_ticket_no);

    $parent_count = count($parent_ticket);
    if($parent_count>3) {
        $parent_money[0] = (float)$l0;
        $parent_money[1] = (float)$l1;
        $parent_money[2] = (float)$l2;

        $redraw = false;
        for ($i=0;$i<3;$i++) {
            $parent = $parent_ticket[$parent_count - $i - 2];
            $query = get_ticket($parent);
            $result = db_query($query);
            $row = mysql_fetch_array($result);

            $value = $row['value'] + $parent_money[$i];

            if($i==0 && $row['max_coupon_value']<=$value) {
                $value=$row['max_coupon_value'];
                $ticket_no=$row['ticket_no'];
                $update_level = -1;
                $query = update_ticket_level_and_value($ticket_no,$update_level,$value);
                $result = db_query($query);
                //echo $query;
                $redraw = true;
            } else {
                $query = update_ticket_value($parent,$value);
                //echo $query;
                $result = db_query($query);
            }
        }

        if($redraw) {
            $query = "SELECT * from ticket WHERE level>=0";
            //echo $query;
            $result = db_query($query);

            $j=0;
            $l=0;
            while($row = mysql_fetch_array($result)) {
                //$parent_ticket_no
                $arr_id[$l][$j]=$row['id'];
                $arr_level[$l][$j]=$l;
                $arr_ticket_no[$l][$j]=$row['ticket_no'];
                $j++;
                if($j>=$total[$l]) {
                    $j=0;
                    $l++;
                }
            }

            for($i=0;$i<=$l;$i++) {
                for($j=0;$j<$total[$i];$j++) {
                    $id = $arr_id[$i][$j];
                    $level = $i;

                    if($i==0) {
                        $parent_ticket_no=0;
                    } else {
                        $parent_ticket_no = $arr_ticket_no[$i-1][$j/3];
                    }
                    $query = "UPDATE ticket
                            SET parent_ticket_no='$parent_ticket_no',
                                level='$level' WHERE id='$id'";

                    //echo $query;
                    $result = db_query($query);
                }
            }

        }
    }
    sem_release($semaphore);

}


?>
